package com.magicsoft.app.helper;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;

import com.magicsoft.app.entity.ContactPerson;

public class ContactHelper {

	private static final String TAG = "ContactHelper";

	// 得到联系人数组
	public static List<ContactPerson> GetPhoneContact(Context context)
			throws Exception {

		List<ContactPerson> lists = new ArrayList<ContactPerson>();

		Uri uri = Uri.parse("content://com.android.contacts/contacts");
		ContentResolver resolver = context.getContentResolver();
		Cursor cursor = resolver.query(uri, new String[] { "_id" }, null, null,
				null);
		while (cursor.moveToNext()) {
			String name = "";
			List<String> list = new ArrayList<String>();

			int contractID = cursor.getInt(0);
			StringBuilder sb = new StringBuilder("contractID=");
			sb.append(contractID);
			uri = Uri.parse("content://com.android.contacts/contacts/"
					+ contractID + "/data");
			Cursor cursor1 = resolver.query(uri, new String[] { "mimetype",
					"data1", "data2" }, null, null, null);
			while (cursor1.moveToNext()) {

				String data1 = cursor1.getString(cursor1
						.getColumnIndex("data1"));
				String mimeType = cursor1.getString(cursor1
						.getColumnIndex("mimetype"));
				if ("vnd.android.cursor.item/name".equals(mimeType)) { // 是姓名
					sb.append(",name=" + data1);
					name = data1;

				} else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { // 邮箱
					sb.append(",email=" + data1);
				} else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { // 手机

					data1 = StringUtils.trimPhone(data1);

					sb.append(",phone=" + data1);
					list.add(data1);

				}
			}
			cursor1.close();

			for (String string : list) {
				ContactPerson person = new ContactPerson();
				person.setMembername(name);
				person.setMobile(string);
				if (StringUtils.isEmpty(person.getMembername()) == false
						&& StringUtils.isEmpty(person.getMobile()) == false) {
					lists.add(person);
				}
			}

			Log.i(TAG, sb.toString());
		}
		cursor.close();

		return lists;

	}

	// 此方法一样可以获取到同名字下多个电话号码
	public List<ContactPerson> printContacts(Context context) {
		// 生成ContentResolver对象
		ContentResolver contentResolver = context.getContentResolver();

		List<ContactPerson> lists = new ArrayList<ContactPerson>();
		// 获得所有的联系人
		/*
		 * Cursor cursor = contentResolver.query(
		 * ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		 */
		// 这段代码和上面代码是等价的，使用两种方式获得联系人的Uri
		Cursor cursor = contentResolver.query(
				Uri.parse("content://com.android.contacts/contacts"), null,
				null, null, null);

		// 循环遍历
		if (cursor.moveToFirst()) {

			int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
			int displayNameColumn = cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

			do {
				// 获得联系人的ID
				String contactId = cursor.getString(idColumn);
				// 获得联系人姓名
				String displayName = cursor.getString(displayNameColumn);

				// 使用Toast技术显示获得的联系人信息
				Toast.makeText(context, "联系人姓名：" + displayName,
						Toast.LENGTH_LONG).show();

				// 查看联系人有多少个号码，如果没有号码，返回0
				int phoneCount = cursor
						.getInt(cursor
								.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

				if (phoneCount > 0) {
					// 获得联系人的电话号码列表
					Cursor phoneCursor = context.getContentResolver().query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
									+ "=" + contactId, null, null);
					if (phoneCursor.moveToFirst()) {
						do {
							// 遍历所有的联系人下面所有的电话号码
							String phoneNumber = phoneCursor
									.getString(phoneCursor
											.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
							// 使用Toast技术显示获得的号码
							ContactPerson person = new ContactPerson();
							person.setMembername(displayName);
							person.setMobile(phoneNumber);
							if (StringUtils.isEmpty(person.getMembername()) == false
									&& StringUtils.isEmpty(person.getMobile()) == false) {
								lists.add(person);
							}

						} while (phoneCursor.moveToNext());
					}
				}

			} while (cursor.moveToNext());
		}
		return lists;

	}

}
